﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using Ortodoctor.Componentes;

namespace Ortodoctor.modelo
{
    class modeloEndereco
    {
        //string connectionstring = ("Server=186.202.152.24;Port=5432;User Id=ortodoctor1;Password=DycbqDfHLI;Database=ortodoctor1;MinPoolSize=5;CommandTimeout=60;");
        string connectionstring = ("Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=ortodoctor;MinPoolSize=5;CommandTimeout=60;"); NpgsqlCommand com;
        NpgsqlConnection conn;
        NpgsqlDataAdapter da;
        //conexao con = new conexao();
        public modeloEndereco()
        {
            conn = new NpgsqlConnection(connectionstring);
            conn.Open();
        }

        public int insert(Endereco e)
        {
            object obj;
            try
            {
                //con.open();
                String sql = "INSERT INTO endereco(rua, numero, cidade, bairro, uf, cep) " +
                             "VALUES (:p1, :p2, :p3, :p4, :p5, :p6)";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("p1", e.Rua);
                com.Parameters.AddWithValue("p2", e.Numero);
                com.Parameters.AddWithValue("p3", e.Cidade);
                com.Parameters.AddWithValue("p4", e.Bairro);
                com.Parameters.AddWithValue("p5", e.Uf);
                com.Parameters.AddWithValue("p6", e.Cep);
                obj = com.ExecuteScalar();
            }
            finally
            {
                conn.Close();
            }
            return obj != null ? (int.Parse(obj.ToString())) : getMaxId();
        }

        public DataTable getAll()
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * from endereco";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        public int getMaxId()
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT max(id_endereco) as id_endereco from endereco";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return Convert.ToInt32(dt.Rows[0]["id_endereco"]);
        }

        public DataTable getEndereco(int id)
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * FORM endereco where id_endereco=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("id", id);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(com);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        public void update(Endereco e)
        {
            try
            {
                //con.open();
                String sql = "UPDATE endereco SET rua=:r, numero=:n, cidade=:c, bairro=:b, uf=:u, " +
                             "cep=:cep WHERE id_endereco=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("r", e.Rua);
                com.Parameters.AddWithValue("n", e.Numero);
                com.Parameters.AddWithValue("c", e.Cidade);
                com.Parameters.AddWithValue("b", e.Bairro);
                com.Parameters.AddWithValue("u", e.Uf);
                com.Parameters.AddWithValue("cep", e.Cep);
                com.Parameters.AddWithValue("id", e.IdEndereco);
                com.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public void delete(int id)
        {
            try
            {
                //con.open();
                String sql = "DELETE FROM endereco WHERE id_endereco=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("id", id);
                com.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public DataTable getEndereco(string rua, string numero, string cidade)
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * FROM endereco where rua = :rua and cidade = :cidade and numero = :numero";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("rua", rua);
                com.Parameters.AddWithValue("numero", numero);
                com.Parameters.AddWithValue("cidade", cidade);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(com);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
    }
}
